package org.example.backend.demos.web.UserModule.service.impl;

import org.example.backend.demos.web.UserModule.dao.UserDao;
import org.example.backend.demos.web.UserModule.entity.LoginResult;
import org.example.backend.demos.web.UserModule.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class myUserDetailsService implements UserDetailsService {
    @Autowired
    private UserDao userDao;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userDao.selectByUsername(username);
        // 检查用户是否存在
        if (user == null) {
            throw new UsernameNotFoundException("无效用户名:" + username);
        }

        return LoginResult.builder().id(user.getId()).username(user.getUsername()).password(user.getPassword()).role(user.getRole()).token(user.getToken()).build();

    }
}
